<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use think\Model;
//产品
class Products extends Common
{
	public function huishou($page=1,$list=10){
		//接收数据
    	$request = Request::instance();

    	
    	$data=$request->post();

		$where="p.is_deleted=0 and p.is_actived=0 and sk.is_deleted=0 and sk.is_actived=1 and s.is_deleted=0 and s.is_actived=1 and c.is_deleted=0 and c.is_actived=1 and b.is_actived=1 and b.is_deleted=0 ";

		$sum=Db::table("pro_products")->alias("p")
		->join("pro_sku sk","p.sku_id=sk.id")
		->join("pro_series s","sk.series_id=s.id")
    	->join("pro_category c","s.category_id=c.id")
    	->join("pro_brands b","c.brand_id=b.id")
    	->where($where)
    	->count();


    	$maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;
        $parr=Db::table("pro_products")->alias("p")
		->join("pro_sku sk","p.sku_id=sk.id")
		->join("pro_series s","sk.series_id=s.id")
    	->join("pro_category c","s.category_id=c.id")
    	->join("pro_brands b","c.brand_id=b.id")
    	->join("pro_price pp","pp.pid=p.id")
    	->where($where)
    	->order("p.id")
    	->limit($start,$list)
        ->field("p.id,p.pro_no,p.pro_name,p.unit,b.brand_name,c.category_name,s.series_name,sk.sku_no,sk.sku_name,p.box_num,pp.factory")
        ->select();
		$this->assign("sum",$sum);
		$this->assign("page",$page);
		$this->assign("list",$list);
		$this->assign("maxpage",$maxpage);
		
		$this->assign("parr",$parr);
		return $this->fetch('huishou');

	}
	public function products($page=1,$list=10){
		//接收数据
    	$request = Request::instance();
        $data=$request->post();
        $where="p.is_deleted=0 and p.is_actived=1 and sk.is_deleted=0 and sk.is_actived=1 and s.is_deleted=0 and s.is_actived=1 and c.is_deleted=0 and c.is_actived=1 and b.is_actived=1 and b.is_deleted=0 ";
        if($data){
        if(trim($data["series_name"]))
    			$where.="and s.series_name like '%{$data["series_name"]}%' ";
    		if(trim($data["category_name"]))
    			$where.="and c.category_name like '%{$data["category_name"]}%' ";
    		if(trim($data["brand_name"]))
    			$where.="and b.brand_name like '%{$data["brand_name"]}%' ";
    		if(trim($data["sku_name"]))
    			$where.="and sk.sku_name like '%{$data["sku_name"]}%' ";
			if(trim($data["pro_no"]))
    			$where.="and p.pro_no like '%{$data["pro_no"]}%' ";
    		if(trim($data["pro_name"]))
    			$where.="and p.pro_name like '%{$data["pro_name"]}%' ";
    		if(trim($data["box_num"])){
    			try{
    				$data["box_num"]=intval($data["box_num"]);
    			}catch(\Exception $e){
                  	$data["box_num"]=0;
             	}
    			$where.="and p.box_num ={$data["box_num"]} ";

    		}
    	}else{
    		$data=["series_name"=>"","category_name"=>"","brand_name"=>"","pro_no"=>"","sku_name"=>"","pro_name"=>"","box_num"=>""];
    	}

		$sum=Db::table("pro_products")->alias("p")
		->join("pro_sku sk","p.sku_id=sk.id")
		->join("pro_series s","sk.series_id=s.id")
    	->join("pro_category c","s.category_id=c.id")
    	->join("pro_brands b","c.brand_id=b.id")
    	->where($where)
    	->count();


    	$maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;
        $parr=Db::table("pro_products")->alias("p")
		->join("pro_sku sk","p.sku_id=sk.id")
		->join("pro_series s","sk.series_id=s.id")
    	->join("pro_category c","s.category_id=c.id")
    	->join("pro_brands b","c.brand_id=b.id")
    	->join("pro_price pp","pp.pid=p.id")
    	->where($where)
    	->order("p.id")
    	->limit($start,$list)
        ->field("p.id,p.pro_no,p.pro_name,p.unit,b.brand_name,c.category_name,s.series_name,sk.sku_no,sk.sku_name,p.box_num,pp.factory")
        ->select();
		$this->assign("sum",$sum);
		$this->assign("page",$page);
		$this->assign("list",$list);
		$this->assign("maxpage",$maxpage);
		$this->assign("data",$data);
		$this->assign("parr",$parr);
		return $this->fetch('products');
	}
	//添加商品
	public function addproduct($pid=0){
		if($pid){
			 $parr=Db::table("pro_products")->alias("p")
		->join("pro_sku sk","p.sku_id=sk.id")
		->join("pro_series s","sk.series_id=s.id")
    	->join("pro_category c","s.category_id=c.id")
    	->join("pro_brands b","c.brand_id=b.id")
    	->join("pro_price pp","pp.pid=p.id")
    	->join("cg_channel_types2 ct","p.channel_type=ct.id","LEFT")
    	->where("p.id=$pid")
    	->field("p.id,p.pro_no,p.pro_name,p.unit,p.weight,p.pro_sku,p.base_unit_id,p.format,p.sku_id,sk.sku_name,p.box_num,p.box_weight,p.box_volume,p.box_size,p.box_no,p.channel_type,p.entry,ct.title,p.remark,pp.factory,pp.box_factory,pp.cost,pp.box_cost,pp.availability,pp.box_availability,pp.retail_standard,pp.box_retail_standard,pp.retail_low,pp.box_retail_low,pp.retail_high,pp.box_retail_high,pp.sell_standard,pp.box_sell_standard,pp.sell_low,pp.box_sell_low,pp.sell_high,pp.box_sell_high,pp.promotion_low,pp.box_promotion_low,pp.promotion_high,pp.box_promotion_high,pp.promotion_shocking,pp.box_promotion_shocking")
    	->find();
    	//print_r($parr);
		}else{
			$parr=[
				"id"=>0,"pro_no"=>"","pro_name"=>"","unit"=>"","weight"=>"","pro_sku"=>"","base_unit_id"=>0,
				"format"=>"","sku_id"=>"","sku_name"=>"","box_num"=>"","box_weight"=>"","box_volume"=>"","box_size"=>"",
				"box_no"=>"","channel_type"=>"","entry"=>1,"title"=>"","remark"=>"","factory"=>"","box_factory"=>"","availability"=>"",
				"cost"=>"","box_cost"=>"","box_availability"=>"","retail_standard"=>"","box_retail_standard"=>"","retail_low"=>"",
				"box_retail_low"=>"","retail_high"=>"","box_retail_high"=>"","sell_standard"=>"","box_sell_standard"=>"","sell_low"=>"",
				"box_sell_low"=>"","sell_high"=>"","box_sell_high"=>"","promotion_low"=>"","box_promotion_low"=>"","promotion_high"=>"",
				"box_promotion_high"=>"","promotion_shocking"=>"","box_promotion_shocking"=>""
			];
		}
		//获取进场限制
		$pro_entry=Db::table("pro_entry")->where(["is_actived"=>1])->select();


		$this->assign("pro_entry",$pro_entry);
		$this->assign("parr",$parr);
		$this->assign("pid",$pid);
		return $this->fetch('addproduct');
	}
	//获得二级渠道
	public function getchannl(){
		 $request = Request::instance();
        
        $data=$request->post();//接收数据
        //print_r($data);exit;
        //$data["cid"]=2;
        $result=[];
        //查询一级渠道
        $res1=Db::table("cg_channel_types1")->select();
		for($i=0;$i<count($res1);$i++){
                $result[$i]["id"]="T".$res1[$i]["id"];
               
                $result[$i]["name"]=$res1[$i]["title"];
                $result[$i]["pId"]=0;
                $result[$i]["nocheck"]=true;
                
           }

        //查询二级渠道
        $res2=Db::table("cg_channel_types2")->select();

            for($i=0;$i<count($res2);$i++){
             
                $result[$i+count($res1)]["id"]=$res2[$i]["id"];
               
                 $result[$i+count($res1)]["name"]=$res2[$i]["title"];
                $result[$i+count($res1)]["pId"]="T".$res2[$i]["pid"];
                 if($data["cid"]==$res2[$i]["id"]){
               		$result[$i+count($res1)]["checked"]=true;
               }
                
           }
            return json_encode($result);
	}
	//添加商品操作
	public function addp(){
		
		$request = Request::instance();
        
        $datas=$request->post();//接收数据
       // print_r($data);exit;
        if(!$datas){
        	return json_encode(["msg"=>"非法进入","state"=>2]);
        }
        //产品表数据
        $pdata["pro_no"]=htmlspecialchars(trim($datas["pro_no"]));
        $pdata["pro_name"]=htmlspecialchars(trim($datas["pro_name"]));
        $pdata["unit"]=htmlspecialchars(trim($datas["unit"]));

        $pdata["weight"]=trim($datas["weight"]);
        $pdata["pro_sku"]=htmlspecialchars(trim($datas["pro_sku"]));
        $pdata["base_unit_id"]=trim($datas["base_unit_id"]);
        $pdata["format"]=htmlspecialchars(trim($datas["format"]));
        //
        $pdata["sku_id"]=trim($datas["sku_id"]);
        $pdata["box_num"]=trim($datas["box_num"]);

        $pdata["box_weight"]=trim($datas["box_weight"]);

        $pdata["box_volume"]=trim($datas["box_volume"]);

        $pdata["box_size"]=trim($datas["box_size"]);
        $pdata["box_no"]=trim($datas["box_no"]);
        $pdata["channel_type"]=trim($datas["channel_type"]);
        $pdata["entry"]=trim($datas["pro_entry"]);
        $pdata["remark"]=htmlspecialchars(trim($datas["remark"]));
        $pdata["c_users_id"]=Session::get("login_id");
        $pdata["created_on"]=date("Y-m-d H:i:s");
        //产品价格表数据
        $datapp["factory"]=trim($datas["factory"]);
        $datapp["cost"]=trim($datas["cost"]);
        $datapp["availability"]=trim($datas["availability"]);
        $datapp["retail_standard"]=trim($datas["retail_standard"]);
        $datapp["retail_low"]=trim($datas["retail_low"]);
        $datapp["retail_high"]=trim($datas["retail_high"]);
        $datapp["sell_standard"]=trim($datas["sell_standard"]);
        $datapp["sell_low"]=trim($datas["sell_low"]);
        $datapp["sell_high"]=trim($datas["sell_high"]);
        $datapp["promotion_shocking"]=trim($datas["promotion_shocking"]);
        $datapp["promotion_low"]=trim($datas["promotion_low"]);
        $datapp["promotion_high"]=trim($datas["promotion_high"]);
        $datapp["box_factory"]=trim($datas["box_factory"]);
        $datapp["box_cost"]=trim($datas["box_cost"]);
        $datapp["box_availability"]=trim($datas["box_availability"]);
        $datapp["box_retail_standard"]=trim($datas["box_retail_standard"]);
        $datapp["box_retail_low"]=trim($datas["box_retail_low"]);
        $datapp["box_retail_high"]=trim($datas["box_retail_high"]);
        $datapp["box_sell_standard"]=trim($datas["box_sell_standard"]);
        $datapp["box_sell_low"]=trim($datas["box_sell_low"]);
        $datapp["box_sell_high"]=trim($datas["box_sell_high"]);
        $datapp["box_promotion_shocking"]=trim($datas["box_promotion_shocking"]);
        $datapp["box_promotion_low"]=trim($datas["box_promotion_low"]);
        $datapp["box_promotion_high"]=trim($datas["box_promotion_high"]);
        //验证
        if(!$pdata["pro_name"]){
        	return json_encode(["msg"=>"产品名不能为空","state"=>2]);
        }
        if(!$pdata["unit"]){
        	return json_encode(["msg"=>"产品零售单位不能为空","state"=>2]);
        }
        if(!$pdata["sku_id"]){
        	return json_encode(["msg"=>"所属SKU必须选择","state"=>2]);
        }
        if(!$pdata["box_num"]){
        	return json_encode(["msg"=>"箱规必须填写","state"=>2]);
        }
        if(!$pdata["base_unit_id"]){
        	return json_encode(["msg"=>"发运及库存基本单位必须选择","state"=>2]);
        }
        if(!($datapp["box_factory"]&&$datapp["factory"])){
        	return json_encode(["msg"=>"出厂价必须填写","state"=>2]);
        }
        if(!$pdata["pro_no"]){
        	$pdata["pro_no"]="Pro".rand(0,100).time().Session::get("login_id");
        }
        Db::startTrans();
        try{
            $ScApi = new ScApi();

        	$res1=Db::table("pro_products")->insertGetId($pdata);
            $pdata['id']=$res1;
            $ScApi->GoodsSave(array_merge($pdata,$datapp),0);
 			$datapp["pid"]=$res1;
        	$res2=Db::table("pro_price")->insert($datapp);
        }catch(\Exception $e){
        	Db::rollBack();
            return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
        }
        if($res1&&$res2){
        	Db::commit();
        	return json_encode(["msg"=>$pdata["pro_name"]."({$pdata["pro_no"]})添加已成功","state"=>1]);
        }else{
        	Db::rollBack();
            return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
        }
	}
	//修改操作
	public function updatep(){

		$request = Request::instance();
        
        $datas=$request->post();//接收数据
       // print_r($data);exit;
        if(!$datas){
        	return json_encode(["msg"=>"非法进入","state"=>2]);
        }
        //产品表数据
        $pdata["id"]=trim($datas["pid"]);
        $pdata["pro_name"]=htmlspecialchars(trim($datas["pro_name"]));
        $pdata["unit"]=htmlspecialchars(trim($datas["unit"]));
       
        	$pdata["weight"]=trim($datas["weight"]);
        $pdata["pro_sku"]=htmlspecialchars(trim($datas["pro_sku"]));
        $pdata["base_unit_id"]=trim($datas["base_unit_id"]);
        $pdata["format"]=htmlspecialchars(trim($datas["format"]));
        //
        $pdata["sku_id"]=trim($datas["sku_id"]);
        $pdata["box_num"]=trim($datas["box_num"]);
       
        $pdata["box_weight"]=trim($datas["box_weight"]);
    	
        $pdata["box_volume"]=trim($datas["box_volume"]);
    	
        $pdata["box_size"]=trim($datas["box_size"]);
        $pdata["box_no"]=trim($datas["box_no"]);
        $pdata["channel_type"]=trim($datas["channel_type"]);
        $pdata["entry"]=trim($datas["pro_entry"]);
        $pdata["remark"]=htmlspecialchars(trim($datas["remark"]));
        $pdata["m_users_id"]=Session::get("login_id");
        $pdata["modifiled_on"]=date("Y-m-d H:i:s");
        //产品价格表数据
        $datapp["factory"]=trim($datas["factory"]);
        $datapp["cost"]=trim($datas["cost"]);
        $datapp["availability"]=trim($datas["availability"]);
        $datapp["retail_standard"]=trim($datas["retail_standard"]);
        $datapp["retail_low"]=trim($datas["retail_low"]);
        $datapp["retail_high"]=trim($datas["retail_high"]);
        $datapp["sell_standard"]=trim($datas["sell_standard"]);
        $datapp["sell_low"]=trim($datas["sell_low"]);
        $datapp["sell_high"]=trim($datas["sell_high"]);
        $datapp["promotion_shocking"]=trim($datas["promotion_shocking"]);
        $datapp["promotion_low"]=trim($datas["promotion_low"]);
        $datapp["promotion_high"]=trim($datas["promotion_high"]);
         $datapp["box_factory"]=trim($datas["box_factory"]);
        $datapp["box_cost"]=trim($datas["box_cost"]);
        $datapp["box_availability"]=trim($datas["box_availability"]);
        $datapp["box_retail_standard"]=trim($datas["box_retail_standard"]);
        $datapp["box_retail_low"]=trim($datas["box_retail_low"]);
        $datapp["box_retail_high"]=trim($datas["box_retail_high"]);
        $datapp["box_sell_standard"]=trim($datas["box_sell_standard"]);
        $datapp["box_sell_low"]=trim($datas["box_sell_low"]);
        $datapp["box_sell_high"]=trim($datas["box_sell_high"]);
        $datapp["box_promotion_shocking"]=trim($datas["box_promotion_shocking"]);
        $datapp["box_promotion_low"]=trim($datas["box_promotion_low"]);
        $datapp["box_promotion_high"]=trim($datas["box_promotion_high"]);
        $datapp["modifiled_on"]=date("Y-m-d H:i:s");
        //验证
        if(!$pdata["pro_name"]){
        	return json_encode(["msg"=>"产品名不能为空","state"=>2]);
        }
        if(!$pdata["unit"]){
        	return json_encode(["msg"=>"产品零售单位不能为空","state"=>2]);
        }
        if(!$pdata["sku_id"]){
        	return json_encode(["msg"=>"所属SKU必须选择","state"=>2]);
        }
        if(!$pdata["box_num"]){
        	return json_encode(["msg"=>"箱规必须填写","state"=>2]);
        }
        if(!$pdata["base_unit_id"]){
        	return json_encode(["msg"=>"发运及库存基本单位必须选择","state"=>2]);
        }
        if(!($datapp["box_factory"]&&$datapp["factory"])){
        	return json_encode(["msg"=>"出厂价必须填写","state"=>2]);
        }
       
        Db::startTrans();
        try{
            $ScApi = new ScApi();
            $ScApi->GoodsSave(array_merge($pdata,$datapp),1);
        	$res1=Db::table("pro_products")->update($pdata);
 			
        	$res2=Db::table("pro_price")->where(["pid"=> $pdata["id"]])->update($datapp);
        	//$res2=1;
        }catch(\Exception $e){
        	Db::rollBack();
            return json_encode(["msg"=>"系统繁忙请稍后再试1","state"=>2]);
        }
        if($res1&&$res2){
        	Db::commit();
        	return json_encode(["msg"=>"操作已成功","state"=>1]);
        }else{
        	Db::rollBack();
            return json_encode(["msg"=>"系统繁忙请稍后再试2","state"=>2]);
        }
	}
	//删除单品
	public function dropp(){
		$request = Request::instance();
        
        $data=$request->post();//接收数据

        //print_r($data);exit;
        //$data["m_users_id"]=Session::get("login_id");
			//$data["modifiled_on"]=date("Y-m-d H:i:s");
		$where=["id"=>$data["pid"],"is_actived"=>0,"m_users_id"=>Session::get("login_id"),"modifiled_on"=>date("Y-m-d H:i:s")];
		$res=Db::table("pro_products")->update($where);
		if($res){
            $ScApi = new ScApi();
            $ScApi->GoodsDel($data["pid"]);
            return 1;
        }

		else{
            return 2;
        }

	}
	//恢复单品
	public function huifup(){
		$request = Request::instance();
        
        $data=$request->post();//接收数据
        //print_r($data);exit;
        //$data["m_users_id"]=Session::get("login_id");
			//$data["modifiled_on"]=date("Y-m-d H:i:s");
		$where=["id"=>$data["pid"],"is_actived"=>1,"m_users_id"=>Session::get("login_id"),"modifiled_on"=>date("Y-m-d H:i:s")];
		$res=Db::table("pro_products")->update($where);
		if($res)
			return 1;
		else
			return 2;
	}
   
}